package com.fw.ui.view.settings.usercontrol.parentmerchant;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.fw.service.bean.EntryBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.parentmerchant.bean.ParentMerchantBean;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.ParentMerchantBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.bean.parentmerchant.SearchMerchantProfileTableItemBean;
import com.fw.ui.core.ViewDescription;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.AbstractView;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.common.util.TableHeaderProperty;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.ViewParentMerchantCloseButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.ViewParentMerchantSelectedMerchantProfileTablePageButtonClickListener;
import com.fw.ui.view.settings.usercontrol.parentmerchant.event.ViewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener;
import com.vaadin.data.util.BeanItem;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table.Align;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@org.springframework.stereotype.Component
@Scope(SCOPE_PROTOTYPE)
@ViewDescription(name = "settings-user-control-parentmerchant-view", path = "/settings/user-control/parentmerchant-view")
public class ViewParentMerchantView extends AbstractView {

    private static final long serialVersionUID = 1L;

    public static final String VIEW_PARENT_MERCHANT_ID_REQUEST_ATTRIBUTE = "VIEW_PARENT_MERCHANT_ID_REQUEST_ATTRIBUTE";

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private MerchantProfileBAO merchantProfileBAO;
    @Autowired
    private ParentMerchantBAO parentMerchantBAO;

    private Map<String, String> merchantStatusMap = new HashMap<String, String>();
    @SuppressWarnings("rawtypes")
    private Map<Long, BeanItem> selectedBeanItemMap = new LinkedHashMap<Long, BeanItem>();

    /**
     * BEAN
     */
    private ParentMerchantBean parentMerchantBean = new ParentMerchantBean();

    /**
     * EVENT
     */
    @Autowired
    private ViewParentMerchantSelectedMerchantProfileTablePageButtonClickListener viewParentMerchantSelectedMerchantProfileTablePageButtonClickListener;
    @Autowired
    private ViewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener viewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener;

    @Autowired
    private ViewParentMerchantCloseButtonClickListener viewParentMerchantCloseButtonClickListener;

    /**
     * COMPONENT
     */
    private Panel viewParentMerchantPanel = new Panel();

    private Panel viewParentMerchantProfilePanel = new Panel();
    private Panel viewParentMerchantContactPersonPanel = new Panel();
    private Panel viewMerchantMemberPanel = new Panel();

    private GridLayout gridParentMerchantPanelLayout = new GridLayout(4, 3);
    private GridLayout gridParentMerchantProfileLayout = new GridLayout(4, 4);
    private GridLayout gridParentMerchantContactPersonLayout = new GridLayout(4, 5);
    private GridLayout gridParentMerchantMemberLayout = new GridLayout(1, 3);

    private Label lblParentMerchantID = new Label();
    private Label lblParentMerchantName = new Label();
    private Label lblBusinessAddress = new Label();
    private Label lblParentStatus = new Label();
    private Label lblTitle = new Label();
    private Label lblFirstName = new Label();
    private Label lblLastName = new Label();
    private Label lblTelephoneNumber = new Label();
    private Label lblEmailAddress = new Label();

    private TextField txtParentMerchantID = new TextField();
    private TextField txtParentMerchantName = new TextField();
    private TextArea txtBusinessAddress = new TextArea();
    private ComboBox cbbParentStatus = new ComboBox();
    private ComboBox cbbTitle = new ComboBox();
    private TextField txtFirstName = new TextField();
    private TextField txtLastName = new TextField();
    private TextField txtTelephoneNumber = new TextField();
    private TextField txtEmailAddress = new TextField();

    @Autowired
    protected TablePanel tableMerchantSelectedPanel;
    private TableHeaderProperty hNoSelected = new TableHeaderProperty();
    private TableHeaderProperty hMerchantIDSelected = new TableHeaderProperty();
    private TableHeaderProperty hMerchantCompanyNameSelected = new TableHeaderProperty();
    private TableHeaderProperty hMerchantUrlSelected = new TableHeaderProperty();
    private TableHeaderProperty hMerchantContactPersonSelected = new TableHeaderProperty();
    private TableHeaderProperty hIdSelected = new TableHeaderProperty();

    private Button btnClose = new Button();

    @Autowired
    private UICommonUtils uiCommonUtils;

    @Override
    public void updateStrings() {

	this.viewParentMerchantPanel.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.panel.viewParentMerchantPanel"));
	this.viewParentMerchantProfilePanel
		.setCaption(this.messageBAO
			.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.panel.viewParentMerchantProfilePanel"));
	this.viewParentMerchantContactPersonPanel
		.setCaption(this.messageBAO
			.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.panel.viewParentMerchantContactPersonPanel"));
	this.tableMerchantSelectedPanel.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.panel.parentMerchantMember"));

	this.lblParentMerchantID.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.parentMerchantID"));
	this.lblParentMerchantName.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.parentMerchantName"));
	this.lblBusinessAddress.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.businessAddress"));
	this.lblParentStatus.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.parentStatus"));
	this.lblTitle.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.title"));
	this.lblFirstName.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.firstName"));
	this.lblLastName.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.lastName"));
	this.lblTelephoneNumber.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.telephoneNumber"));
	this.lblEmailAddress.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.label.emailAddress"));

	this.hNoSelected.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.header.no"));
	this.hMerchantIDSelected.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.header.merchantID"));
	this.hMerchantCompanyNameSelected.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.header.merchantCompnayName"));
	this.hMerchantUrlSelected.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.header.merchantUrl"));
	this.hMerchantContactPersonSelected.setColumnName(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.header.merchantContactPerson"));

	tableMerchantSelectedPanel.setTableColumnHeader(this.hNoSelected.getColumnId(), this.hNoSelected.getColumnName());
	tableMerchantSelectedPanel.setTableColumnHeader(this.hMerchantIDSelected.getColumnId(), this.hMerchantIDSelected.getColumnName());
	tableMerchantSelectedPanel.setTableColumnHeader(this.hMerchantCompanyNameSelected.getColumnId(),
		this.hMerchantCompanyNameSelected.getColumnName());
	tableMerchantSelectedPanel.setTableColumnHeader(this.hMerchantUrlSelected.getColumnId(), this.hMerchantUrlSelected.getColumnName());
	tableMerchantSelectedPanel.setTableColumnHeader(this.hMerchantContactPersonSelected.getColumnId(),
		this.hMerchantContactPersonSelected.getColumnName());

	this.btnClose.setCaption(this.messageBAO
		.getMessage("com.fw.ui.view.settings.usercontrol.parentmerchant.ViewParentMerchantView.button.close"));
    }

    @Override
    public void init() {
	setSizeFull();

	viewParentMerchantPanel.setSizeFull();
	viewParentMerchantPanel.setId("viewParentMerchantPanel");

	viewParentMerchantProfilePanel.setSizeFull();
	viewParentMerchantProfilePanel.setId("viewParentMerchantProfilePanel");

	viewParentMerchantContactPersonPanel.setSizeFull();
	viewParentMerchantContactPersonPanel.setId("viewParentMerchantContactPersonPanel");

	viewMerchantMemberPanel.setSizeFull();
	viewMerchantMemberPanel.setId("viewMerchantMemberPanel");

	String labelWidth = "150px";
	lblParentMerchantID.setWidth(labelWidth);
	lblParentMerchantName.setWidth(labelWidth);
	lblBusinessAddress.setWidth(labelWidth);
	lblParentStatus.setWidth(labelWidth);
	lblTitle.setWidth(labelWidth);
	lblFirstName.setWidth(labelWidth);
	lblLastName.setWidth(labelWidth);
	lblTelephoneNumber.setWidth(labelWidth);
	lblEmailAddress.setWidth(labelWidth);

	String inputWidth = "200px";
	txtParentMerchantID.setWidth(inputWidth);
	txtParentMerchantName.setWidth(inputWidth);
	txtBusinessAddress.setWidth(inputWidth);
	cbbParentStatus.setWidth(inputWidth);
	cbbTitle.setWidth(inputWidth);
	txtFirstName.setWidth(inputWidth);
	txtLastName.setWidth(inputWidth);
	txtTelephoneNumber.setWidth(inputWidth);
	txtEmailAddress.setWidth(inputWidth);

	viewParentMerchantPanel.setContent(gridParentMerchantPanelLayout);
	gridParentMerchantPanelLayout.setSizeFull();
	gridParentMerchantPanelLayout.setColumnExpandRatio(0, 5);
	gridParentMerchantPanelLayout.setColumnExpandRatio(1, 45);
	gridParentMerchantPanelLayout.setColumnExpandRatio(2, 45);
	gridParentMerchantPanelLayout.setColumnExpandRatio(3, 5);
	gridParentMerchantPanelLayout.addComponent(viewParentMerchantProfilePanel, 1, 0);
	gridParentMerchantPanelLayout.setComponentAlignment(viewParentMerchantProfilePanel, Alignment.TOP_LEFT);
	gridParentMerchantPanelLayout.addComponent(viewParentMerchantContactPersonPanel, 2, 0);
	gridParentMerchantPanelLayout.setComponentAlignment(viewParentMerchantContactPersonPanel, Alignment.TOP_LEFT);
	gridParentMerchantPanelLayout.addComponent(viewMerchantMemberPanel, 0, 1, 3, 1);
	gridParentMerchantPanelLayout.setComponentAlignment(viewMerchantMemberPanel, Alignment.TOP_CENTER);

	viewParentMerchantProfilePanel.setContent(gridParentMerchantProfileLayout);
	gridParentMerchantProfileLayout.setSizeFull();
	gridParentMerchantProfileLayout.setSpacing(true);
	gridParentMerchantProfileLayout.setMargin(true);
	gridParentMerchantProfileLayout.setColumnExpandRatio(0, 10);
	gridParentMerchantProfileLayout.setColumnExpandRatio(1, 20);
	gridParentMerchantProfileLayout.setColumnExpandRatio(2, 60);
	gridParentMerchantProfileLayout.setColumnExpandRatio(3, 10);
	gridParentMerchantProfileLayout.setRowExpandRatio(0, 2);
	gridParentMerchantProfileLayout.setRowExpandRatio(1, 1);
	gridParentMerchantProfileLayout.setRowExpandRatio(2, 1);
	gridParentMerchantProfileLayout.setRowExpandRatio(3, 2);
	gridParentMerchantProfileLayout.addComponent(lblParentMerchantID, 1, 0);
	gridParentMerchantProfileLayout.setComponentAlignment(lblParentMerchantID, Alignment.TOP_LEFT);
	VerticalLayout parentMerchantIdLayout = new VerticalLayout();
	parentMerchantIdLayout.addComponent(txtParentMerchantID);
	gridParentMerchantProfileLayout.addComponent(parentMerchantIdLayout, 2, 0);
	gridParentMerchantProfileLayout.setComponentAlignment(parentMerchantIdLayout, Alignment.TOP_LEFT);
	gridParentMerchantProfileLayout.addComponent(lblParentMerchantName, 1, 1);
	gridParentMerchantProfileLayout.setComponentAlignment(lblParentMerchantName, Alignment.TOP_LEFT);
	VerticalLayout parentMerchantNameLayout = new VerticalLayout();
	parentMerchantNameLayout.addComponent(txtParentMerchantName);
	gridParentMerchantProfileLayout.addComponent(parentMerchantNameLayout, 2, 1);
	gridParentMerchantProfileLayout.setComponentAlignment(parentMerchantNameLayout, Alignment.TOP_LEFT);
	gridParentMerchantProfileLayout.addComponent(lblBusinessAddress, 1, 2);
	gridParentMerchantProfileLayout.setComponentAlignment(lblBusinessAddress, Alignment.TOP_LEFT);
	VerticalLayout businessAddressLayout = new VerticalLayout();
	businessAddressLayout.addComponent(txtBusinessAddress);
	gridParentMerchantProfileLayout.addComponent(businessAddressLayout, 2, 2);
	gridParentMerchantProfileLayout.setComponentAlignment(businessAddressLayout, Alignment.TOP_LEFT);
	gridParentMerchantProfileLayout.addComponent(lblParentStatus, 1, 3);
	gridParentMerchantProfileLayout.setComponentAlignment(lblParentStatus, Alignment.TOP_LEFT);
	VerticalLayout parentStatusLayout = new VerticalLayout();
	parentStatusLayout.addComponent(cbbParentStatus);
	gridParentMerchantProfileLayout.addComponent(parentStatusLayout, 2, 3);
	gridParentMerchantProfileLayout.setComponentAlignment(parentStatusLayout, Alignment.TOP_LEFT);

	viewParentMerchantContactPersonPanel.setContent(gridParentMerchantContactPersonLayout);
	gridParentMerchantContactPersonLayout.setSizeFull();
	gridParentMerchantContactPersonLayout.setSpacing(true);
	gridParentMerchantContactPersonLayout.setMargin(true);
	gridParentMerchantContactPersonLayout.setColumnExpandRatio(0, 10);
	gridParentMerchantContactPersonLayout.setColumnExpandRatio(1, 20);
	gridParentMerchantContactPersonLayout.setColumnExpandRatio(2, 60);
	gridParentMerchantContactPersonLayout.setColumnExpandRatio(3, 10);
	gridParentMerchantContactPersonLayout.setRowExpandRatio(0, 2);
	gridParentMerchantContactPersonLayout.setRowExpandRatio(1, 1);
	gridParentMerchantContactPersonLayout.setRowExpandRatio(2, 1);
	gridParentMerchantContactPersonLayout.setRowExpandRatio(3, 1);
	gridParentMerchantContactPersonLayout.setRowExpandRatio(4, 2);
	gridParentMerchantContactPersonLayout.addComponent(lblTitle, 1, 0);
	gridParentMerchantContactPersonLayout.setComponentAlignment(lblTitle, Alignment.TOP_LEFT);
	VerticalLayout titleLayout = new VerticalLayout();
	titleLayout.addComponent(cbbTitle);
	gridParentMerchantContactPersonLayout.addComponent(titleLayout, 2, 0);
	gridParentMerchantContactPersonLayout.setComponentAlignment(titleLayout, Alignment.TOP_LEFT);
	gridParentMerchantContactPersonLayout.addComponent(lblFirstName, 1, 1);
	gridParentMerchantContactPersonLayout.setComponentAlignment(lblFirstName, Alignment.TOP_LEFT);
	VerticalLayout firstNameLayout = new VerticalLayout();
	firstNameLayout.addComponent(txtFirstName);
	gridParentMerchantContactPersonLayout.addComponent(firstNameLayout, 2, 1);
	gridParentMerchantContactPersonLayout.setComponentAlignment(firstNameLayout, Alignment.TOP_LEFT);
	gridParentMerchantContactPersonLayout.addComponent(lblLastName, 1, 2);
	gridParentMerchantContactPersonLayout.setComponentAlignment(lblLastName, Alignment.TOP_LEFT);
	VerticalLayout lastNameLayout = new VerticalLayout();
	lastNameLayout.addComponent(txtLastName);
	gridParentMerchantContactPersonLayout.addComponent(lastNameLayout, 2, 2);
	gridParentMerchantContactPersonLayout.setComponentAlignment(lastNameLayout, Alignment.TOP_LEFT);
	gridParentMerchantContactPersonLayout.addComponent(lblTelephoneNumber, 1, 3);
	gridParentMerchantContactPersonLayout.setComponentAlignment(lblTelephoneNumber, Alignment.TOP_LEFT);
	VerticalLayout telephoneNumberLayout = new VerticalLayout();
	telephoneNumberLayout.addComponent(txtTelephoneNumber);
	gridParentMerchantContactPersonLayout.addComponent(telephoneNumberLayout, 2, 3);
	gridParentMerchantContactPersonLayout.setComponentAlignment(telephoneNumberLayout, Alignment.TOP_LEFT);
	gridParentMerchantContactPersonLayout.addComponent(lblEmailAddress, 1, 4);
	gridParentMerchantContactPersonLayout.setComponentAlignment(lblEmailAddress, Alignment.TOP_LEFT);
	VerticalLayout emailAddressLayout = new VerticalLayout();
	emailAddressLayout.addComponent(txtEmailAddress);
	gridParentMerchantContactPersonLayout.addComponent(emailAddressLayout, 2, 4);
	gridParentMerchantContactPersonLayout.setComponentAlignment(emailAddressLayout, Alignment.TOP_LEFT);

	viewMerchantMemberPanel.setContent(gridParentMerchantMemberLayout);
	gridParentMerchantMemberLayout.setSizeFull();
	gridParentMerchantMemberLayout.setSpacing(true);
	gridParentMerchantMemberLayout.setMargin(true);

	hNoSelected.setColumnId("seqNo");
	hNoSelected.setType(Integer.class);
	hNoSelected.setAlignment(Align.CENTER);
	hNoSelected.setDefaultValue(null);
	hNoSelected.setIconResource(null);
	hNoSelected.setWidth(50);
	hNoSelected.setOrder(1);

	hMerchantIDSelected.setColumnId("merchantID");
	hMerchantIDSelected.setType(String.class);
	hMerchantIDSelected.setAlignment(Align.LEFT);
	hMerchantIDSelected.setDefaultValue(null);
	hMerchantIDSelected.setIconResource(null);
	hMerchantIDSelected.setWidth(100);
	hMerchantIDSelected.setOrder(2);

	hMerchantCompanyNameSelected.setColumnId("merchantCompnayName");
	hMerchantCompanyNameSelected.setType(String.class);
	hMerchantCompanyNameSelected.setAlignment(Align.LEFT);
	hMerchantCompanyNameSelected.setDefaultValue(null);
	hMerchantCompanyNameSelected.setIconResource(null);
	hMerchantCompanyNameSelected.setWidth(300);
	hMerchantCompanyNameSelected.setOrder(3);

	hMerchantUrlSelected.setColumnId("merchantUrl");
	hMerchantUrlSelected.setType(String.class);
	hMerchantUrlSelected.setAlignment(Align.LEFT);
	hMerchantUrlSelected.setDefaultValue(null);
	hMerchantUrlSelected.setIconResource(null);
	hMerchantUrlSelected.setOrder(4);

	hMerchantContactPersonSelected.setColumnId("merchantContactPerson");
	hMerchantContactPersonSelected.setType(String.class);
	hMerchantContactPersonSelected.setAlignment(Align.LEFT);
	hMerchantContactPersonSelected.setDefaultValue(null);
	hMerchantContactPersonSelected.setIconResource(null);
	hMerchantContactPersonSelected.setWidth(300);
	hMerchantContactPersonSelected.setOrder(5);

	hIdSelected.setColumnId("id");
	hIdSelected.setType(Long.class);
	hIdSelected.setVisible(false);
	hIdSelected.setOrder(6);

	List<TableHeaderProperty> headerSelectedProperties = new ArrayList<TableHeaderProperty>();
	headerSelectedProperties.add(hNoSelected);
	headerSelectedProperties.add(hMerchantIDSelected);
	headerSelectedProperties.add(hMerchantCompanyNameSelected);
	headerSelectedProperties.add(hMerchantUrlSelected);
	headerSelectedProperties.add(hMerchantContactPersonSelected);
	headerSelectedProperties.add(hIdSelected);

	tableMerchantSelectedPanel.setTableBeanContainerColumnHeader(headerSelectedProperties, SearchMerchantProfileTableItemBean.class);
	tableMerchantSelectedPanel.setRowIndexVisiblePageNumber();
	tableMerchantSelectedPanel.setTableSelect(false);
	tableMerchantSelectedPanel.setAllowSortRecord(false);
	tableMerchantSelectedPanel.setEnableTableSorting(false);
	tableMerchantSelectedPanel.setTableColumnReordering(false);
	tableMerchantSelectedPanel.createControlTablePageNumber();

	gridParentMerchantMemberLayout.addComponent(tableMerchantSelectedPanel, 0, 2);
	gridParentMerchantMemberLayout.setComponentAlignment(tableMerchantSelectedPanel, Alignment.TOP_CENTER);

	HorizontalLayout commandLayout = new HorizontalLayout();
	commandLayout.setSizeUndefined();
	commandLayout.setSpacing(true);
	commandLayout.setMargin(true);
	this.btnClose.setId("btnClose" + UICommonUtils.EXCLUDE_ENABLE_COMPONENT_PREFIX);
	this.btnClose.addStyleName("button-blue");
	this.btnClose.setDisableOnClick(true);
	this.btnClose.setWidth("70px");
	commandLayout.addComponent(btnClose);
	gridParentMerchantPanelLayout.addComponent(commandLayout, 0, 2, 3, 2);
	gridParentMerchantPanelLayout.setComponentAlignment(commandLayout, Alignment.MIDDLE_CENTER);

	this.addComponent(this.viewParentMerchantPanel);
	this.setComponentAlignment(this.viewParentMerchantPanel, Alignment.TOP_CENTER);
    }

    @Override
    public void initListener() {
	this.viewParentMerchantCloseButtonClickListener.setBasedComponent(this);
	this.viewParentMerchantSelectedMerchantProfileTablePageButtonClickListener.setBasedComponent(this);
	this.viewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener.setBasedComponent(this);

	this.viewParentMerchantCloseButtonClickListener.setAutoEnabled(true);

	this.viewParentMerchantSelectedMerchantProfileTablePageButtonClickListener.setTablePanel(this.tableMerchantSelectedPanel);
	this.viewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener.setTablePanel(this.tableMerchantSelectedPanel);
	this.tableMerchantSelectedPanel
		.addPageTableButtonClickListener(this.viewParentMerchantSelectedMerchantProfileTablePageButtonClickListener);
	this.tableMerchantSelectedPanel
		.addSizeTableValueChangeListener(this.viewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener);

	this.btnClose.addClickListener(this.viewParentMerchantCloseButtonClickListener);
    }

    @Override
    public void initValidator() {
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void initData() {
	try {
	    Long parentMerchantId = (Long) VaadinService.getCurrentRequest().getAttribute(VIEW_PARENT_MERCHANT_ID_REQUEST_ATTRIBUTE);
	    if (parentMerchantId == null) {
		String landing = propertyBAO.getPropertyValue("view.settings-user-control-parentmerchant-search.path");
		this.getUI().getNavigator().navigateTo(landing);
	    } else {
		ParentMerchantBean resultParentMerchantBean = parentMerchantBAO.getParentMerchant(parentMerchantId);
		if (resultParentMerchantBean == null || resultParentMerchantBean.getId() == null) {
		    String msg = this.messageBAO.getMessage("message.error.common");
		    Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
		    notification.setDelayMsec(1000);
		    notification.show(Page.getCurrent());
		} else {
		    List<EntryBean<String, String>> masterData = this.masterDataBAO.selectAllParentMerchantStatus();
		    if (masterData != null) {
			for (EntryBean<String, String> entry : masterData) {
			    this.cbbParentStatus.addItem(entry.getKey());
			    this.cbbParentStatus.setItemCaption(entry.getKey(), entry.getValue());
			}
		    }

		    masterData = this.masterDataBAO.selectAllTitleName();
		    if (masterData != null) {
			for (EntryBean<String, String> entry : masterData) {
			    this.cbbTitle.addItem(entry.getKey());
			    this.cbbTitle.setItemCaption(entry.getKey(), entry.getValue());
			}
		    }

		    uiCommonUtils.setValueUIComponent(this.getTxtParentMerchantID(), resultParentMerchantBean.getParentId());
		    uiCommonUtils.setValueUIComponent(this.getTxtParentMerchantName(), resultParentMerchantBean.getParentName());
		    uiCommonUtils.setValueUIComponent(this.getTxtBusinessAddress(), resultParentMerchantBean.getParentBusniessAddress());
		    uiCommonUtils.setValueUIComponent(this.getCbbParentStatus(), resultParentMerchantBean.getParentStatus());
		    uiCommonUtils.setValueUIComponent(this.getCbbTitle(), resultParentMerchantBean.getContactPersonTitle());
		    uiCommonUtils.setValueUIComponent(this.getTxtFirstName(), resultParentMerchantBean.getContactPersonFirstName());
		    uiCommonUtils.setValueUIComponent(this.getTxtLastName(), resultParentMerchantBean.getContactPersonLastName());
		    uiCommonUtils.setValueUIComponent(this.getTxtTelephoneNumber(),
			    resultParentMerchantBean.getContactPersonTelephoneNumber());
		    uiCommonUtils.setValueUIComponent(this.getTxtEmailAddress(), resultParentMerchantBean.getContactPersonEmailAddress());

		    Set<MerchantProfileBean> merchantProfileBeans = resultParentMerchantBean.getMerchantProfileBeans();
		    this.getSelectedBeanItemMap().clear();
		    if (merchantProfileBeans != null && !merchantProfileBeans.isEmpty()) {
			for (MerchantProfileBean merchantProfileBean : merchantProfileBeans) {
			    SearchMerchantProfileTableItemBean searchMerchantProfileTableItemBean = new SearchMerchantProfileTableItemBean();
			    searchMerchantProfileTableItemBean.setMerchantID(merchantProfileBean.getMerchantId());
			    searchMerchantProfileTableItemBean.setMerchantCompnayName(merchantProfileBean.getCompanyNameEn());
			    searchMerchantProfileTableItemBean.setMerchantUrl(merchantProfileBean.getUrl());
			    searchMerchantProfileTableItemBean.setMerchantContactPerson(merchantProfileBean.getContact1Name());
			    this.getSelectedBeanItemMap()
				    .put(merchantProfileBean.getId(), new BeanItem(searchMerchantProfileTableItemBean));
			}
		    }

		    Map<Long, BeanItem> selectedBeanItemMap = this.getSelectedBeanItemMap();
		    List<BeanItem> selectedBeanItems = new ArrayList<BeanItem>(selectedBeanItemMap.values());
		    int totalElements = selectedBeanItems.size();
		    Integer itemPerPage = this.getTableMerchantSelectedPanel().getManualPageSize();
		    Integer totalPage = Double.valueOf(Math.ceil(Double.valueOf(totalElements) / Double.valueOf(itemPerPage))).intValue();
		    List<BeanItem> subItems = new ArrayList<BeanItem>();
		    if (totalElements > 1) {
			subItems = selectedBeanItems.subList((totalPage - 1) * itemPerPage, totalElements);
		    } else {
			subItems = selectedBeanItems;
		    }

		    this.getTableMerchantSelectedPanel().clearTableData();
		    this.getTableMerchantSelectedPanel().setManualPageTable(1);
		    this.getTableMerchantSelectedPanel().setTableBeanContainerDataTotalPage(subItems, totalPage,
			    Long.valueOf(totalElements));
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}

	uiCommonUtils.setEnableAllUIComponent(viewParentMerchantProfilePanel, false);
	uiCommonUtils.setEnableAllUIComponent(viewParentMerchantContactPersonPanel, false);
    }

    @SuppressWarnings("rawtypes")
    public List<BeanItem> generateTableItemBean(SearchResultBean<MerchantProfileBean> searchResultBean) {
	List<BeanItem> beanItems = new ArrayList<BeanItem>();
	if (searchResultBean != null && searchResultBean.getContent() != null) {

	    for (int loop = 0; loop < searchResultBean.getContent().size(); loop++) {
		MerchantProfileBean merchantProfileBean = searchResultBean.getContent().get(loop);
		SearchMerchantProfileTableItemBean searchMerchantProfileTableItemBean = new SearchMerchantProfileTableItemBean();

		searchMerchantProfileTableItemBean.setId(merchantProfileBean.getId());
		searchMerchantProfileTableItemBean.setMerchantID(merchantProfileBean.getMerchantId());
		searchMerchantProfileTableItemBean.setMerchantCompnayName(merchantProfileBean.getCompanyNameEn());
		searchMerchantProfileTableItemBean.setMerchantUrl(merchantProfileBean.getUrl());
		searchMerchantProfileTableItemBean.setMerchantContactPerson(merchantProfileBean.getContact1Name());

		String statusText = this.merchantStatusMap.get(merchantProfileBean.getMerchantStatus());
		if (statusText == null) {
		    statusText = merchantProfileBean.getMerchantStatus();
		}
		searchMerchantProfileTableItemBean.setMerchantStatus(statusText);

		beanItems.add(new BeanItem<SearchMerchantProfileTableItemBean>(searchMerchantProfileTableItemBean));
	    }
	}
	return beanItems;
    }

    public MasterDataBAO getMasterDataBAO() {
	return masterDataBAO;
    }

    public PropertyBAO getPropertyBAO() {
	return propertyBAO;
    }

    public MessageBAO getMessageBAO() {
	return messageBAO;
    }

    public MerchantProfileBAO getMerchantProfileBAO() {
	return merchantProfileBAO;
    }

    public ParentMerchantBAO getParentMerchantBAO() {
	return parentMerchantBAO;
    }

    public Map<String, String> getMerchantStatusMap() {
	return merchantStatusMap;
    }

    @SuppressWarnings("rawtypes")
    public Map<Long, BeanItem> getSelectedBeanItemMap() {
	return selectedBeanItemMap;
    }

    public ParentMerchantBean getParentMerchantBean() {
	return parentMerchantBean;
    }

    public ViewParentMerchantSelectedMerchantProfileTablePageButtonClickListener getViewParentMerchantSelectedMerchantProfileTablePageButtonClickListener() {
	return viewParentMerchantSelectedMerchantProfileTablePageButtonClickListener;
    }

    public ViewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener getViewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener() {
	return viewParentMerchantSelectedMerchantProfileTableSizeValueChangeListener;
    }

    public ViewParentMerchantCloseButtonClickListener getViewParentMerchantCloseButtonClickListener() {
	return viewParentMerchantCloseButtonClickListener;
    }

    public Panel getViewParentMerchantPanel() {
	return viewParentMerchantPanel;
    }

    public Panel getViewParentMerchantProfilePanel() {
	return viewParentMerchantProfilePanel;
    }

    public Panel getViewParentMerchantContactPersonPanel() {
	return viewParentMerchantContactPersonPanel;
    }

    public Panel getViewMerchantMemberPanel() {
	return viewMerchantMemberPanel;
    }

    public GridLayout getGridParentMerchantPanelLayout() {
	return gridParentMerchantPanelLayout;
    }

    public GridLayout getGridParentMerchantProfileLayout() {
	return gridParentMerchantProfileLayout;
    }

    public GridLayout getGridParentMerchantContactPersonLayout() {
	return gridParentMerchantContactPersonLayout;
    }

    public GridLayout getGridParentMerchantMemberLayout() {
	return gridParentMerchantMemberLayout;
    }

    public Label getLblParentMerchantID() {
	return lblParentMerchantID;
    }

    public Label getLblParentMerchantName() {
	return lblParentMerchantName;
    }

    public Label getLblBusinessAddress() {
	return lblBusinessAddress;
    }

    public Label getLblParentStatus() {
	return lblParentStatus;
    }

    public Label getLblTitle() {
	return lblTitle;
    }

    public Label getLblFirstName() {
	return lblFirstName;
    }

    public Label getLblLastName() {
	return lblLastName;
    }

    public Label getLblTelephoneNumber() {
	return lblTelephoneNumber;
    }

    public Label getLblEmailAddress() {
	return lblEmailAddress;
    }

    public TextField getTxtParentMerchantID() {
	return txtParentMerchantID;
    }

    public TextField getTxtParentMerchantName() {
	return txtParentMerchantName;
    }

    public TextArea getTxtBusinessAddress() {
	return txtBusinessAddress;
    }

    public ComboBox getCbbParentStatus() {
	return cbbParentStatus;
    }

    public ComboBox getCbbTitle() {
	return cbbTitle;
    }

    public TextField getTxtFirstName() {
	return txtFirstName;
    }

    public TextField getTxtLastName() {
	return txtLastName;
    }

    public TextField getTxtTelephoneNumber() {
	return txtTelephoneNumber;
    }

    public TextField getTxtEmailAddress() {
	return txtEmailAddress;
    }

    public TablePanel getTableMerchantSelectedPanel() {
	return tableMerchantSelectedPanel;
    }

    public TableHeaderProperty gethNoSelected() {
	return hNoSelected;
    }

    public TableHeaderProperty gethMerchantIDSelected() {
	return hMerchantIDSelected;
    }

    public TableHeaderProperty gethMerchantCompanyNameSelected() {
	return hMerchantCompanyNameSelected;
    }

    public TableHeaderProperty gethMerchantUrlSelected() {
	return hMerchantUrlSelected;
    }

    public TableHeaderProperty gethMerchantContactPersonSelected() {
	return hMerchantContactPersonSelected;
    }

    public TableHeaderProperty gethIdSelected() {
	return hIdSelected;
    }

    public Button getBtnClose() {
	return btnClose;
    }

    public UICommonUtils getUiCommonUtils() {
	return uiCommonUtils;
    }

}
